package com.situ.ssm.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.ssm.dao.CourseMapper;
import com.situ.ssm.entity.Course;
import com.situ.ssm.entity.PageBean;
import com.situ.ssm.service.ICourseService;
import com.situ.ssm.vo.CourseSearchCondition;

@Service
public class CourseServiceImpl implements ICourseService {
	@Autowired
	private CourseMapper courseDao;
	
	@Override
	public List<Course> list() {
		return courseDao.list();
	}
	
	@Override
	public PageBean<Map<String, Object>> getPageBean(CourseSearchCondition condition) {
		PageBean<Map<String, Object>> pageBean = new PageBean<>();
		pageBean.setPageNo(condition.getPageNo());
		pageBean.setPageSize(condition.getPageSize());;
		//总记录数
		Integer totalCount = courseDao.getTotalCount(condition);
		pageBean.setTotalCount(totalCount);
		//总页数
		Integer totalPage = (int)Math.ceil((double)totalCount/condition.getPageSize());
		pageBean.setTotalPage(totalPage);
		//当前页的数据
		Integer offset = (condition.getPageNo() - 1) * condition.getPageSize();
		List<Map<String, Object>> list = courseDao.findPageBeanList(condition,offset);
		pageBean.setList(list);
		return pageBean;
	}

	@Override
	public boolean insert(Course course) {
		Integer count  = courseDao.insert(course);
		return count == 1 ? true : false;
	}

	@Override
	public boolean deleteById(Integer id) {
		Integer count = courseDao.deleteById(id);
		return count == 1 ? true : false;
	}

	@Override
	public boolean deleteAll(String[] selectIds) {
		Integer count = courseDao.deleteAll(selectIds);
		return count > 0 ? true : false;
	}

	@Override
	public Course findByName(Integer id) {
		return courseDao.findByName(id);
	}

	@Override
	public boolean update(Course course) {
		Integer count = courseDao.update(course);
		return count == 1 ? true : false;
	}

	@Override
	public boolean checkName(String name) {
		Integer count = courseDao.checkName(name);
		return count == 1 ? true : false;
	}


}
